#define __IIC_C__
#include "iic.h"
/*
function: iic initialize
*/
#define IIC_rst() 	{ RIIC0.ICCR1.BIT.IICRST = 1;RIIC0.ICCR1.BIT.IICRST = 0;}
#define IIC_off() 	{ RIIC0.ICCR1.BIT.ICE = 0;}
#define IIC_on() 	{ RIIC0.ICCR1.BIT.ICE = 1;}
#define IIC_marst() { RIIC0.ICMR1.BIT.MTWP = 1;RIIC0.ICCR2.BIT.MST = 1;RIIC0.ICMR1.BIT.MTWP = 0;}
#define IIC_rece()	{ RIIC0.ICMR1.BIT.MTWP = 1;RIIC0.ICCR2.BIT.TRS = 0;RIIC0.ICMR1.BIT.MTWP = 0;}
#define IIC_send()	{ RIIC0.ICMR1.BIT.MTWP = 1;RIIC0.ICCR2.BIT.TRS = 1;RIIC0.ICMR1.BIT.MTWP = 0;}
#define IIC_set_ACKBT() {RIIC0.ICMR3.BIT.ACKWP=1;RIIC0.ICMR3.BIT.ACKBT=1;RIIC0.ICMR3.BIT.ACKWP=0;}

void iic_init(void)
{

	/*turn on iic module*/
	SYSTEM.MSTPCRB.BIT.MSTPB21 = 1;
	SYSTEM.MSTPCRB.BIT.MSTPB21 = 0;
//	
	IIC_on();
	IIC_off();
	IIC_rst();
//	PORTB.DR.BIT.B2=1;
//	PORTB.DR.BIT.B1=1;
//	IIC_rst();
//	PORTB.DR.BIT.B2=0;
//	PORTB.DR.BIT.B1=0;
	/*--buad--*/
	IIC_rst();
	RIIC0.ICMR1.BIT.CKS = 0x6;//IIC clk=PCLK/64
	RIIC0.ICBRL.BYTE = 10;//low level =10*PCLK/64
	RIIC0.ICBRH.BYTE = 10;//low level =10*PCLK/64
//	RIIC0.ICSER.BYTE = 8;
//  RIIC0.ICMR2.BIT.TMOH = 0;
//  RIIC0.ICMR2.BIT.TMOL = 0;
	IIC_marst();
	IIC_on();
//	PORTB.DR.BIT.B2=0;
//	
}

unsigned char iic_start(void)
{
	if(RIIC0.ICCR2.BIT.BBSY==1)
	{
		iic_init();
	}
	if(RIIC0.ICCR2.BIT.BBSY==1)
	{
		return ERROR;
	}
	RIIC0.ICSR2.BIT.START=0;
	RIIC0.ICCR2.BIT.ST = 1;
	while(!RIIC0.ICSR2.BIT.START);
	return OK;
}

unsigned char iic_restart(void)
{
	if(RIIC0.ICCR2.BIT.BBSY==1)
	{
		RIIC0.ICSR2.BIT.START=0;
		RIIC0.ICCR2.BIT.RS = 1;
		while(!RIIC0.ICSR2.BIT.START);
		return OK;
	}
	return ERROR;
}

void iic_stop(void)
{

	RIIC0.ICSR2.BIT.STOP=0;
	RIIC0.ICCR2.BIT.SP = 1;
	while(!RIIC0.ICSR2.BIT.STOP);
	RIIC0.ICSR2.BIT.STOP=0;
	RIIC0.ICSR2.BIT.NACKF=0;
}

void iic_rstop(void)
{

}


unsigned char iic_send_dat(unsigned char *dat,unsigned short datlen)
{
	unsigned int i;

	for(i=0;i<datlen;i++)
	{
		if(RIIC0.ICSR2.BIT.NACKF==1)return ERROR;
		while(!RIIC0.ICSR2.BIT.TDRE);
		RIIC0.ICDRT = *dat;
		dat++;
	}
	while(!RIIC0.ICSR2.BIT.TEND);
	return OK;
}

unsigned char iic_rece_dat(unsigned char daddr,unsigned char *dat,unsigned short datlen)
{
	if(datlen==0)return ERROR;	
	while(!RIIC0.ICSR2.BIT.TDRE);
	RIIC0.ICDRT = daddr;
	while(!RIIC0.ICSR2.BIT.RDRF);
    if(1==RIIC0.ICSR2.BIT.NACKF)
	{
		RIIC0.ICSR2.BIT.STOP=0;
		RIIC0.ICCR2.BIT.SP = 1;
		*dat=RIIC0.ICDRR;
		while(!RIIC0.ICSR2.BIT.STOP);
		RIIC0.ICSR2.BIT.NACKF=0;
		RIIC0.ICSR2.BIT.STOP=0;
		return ERROR;
	}
	if(datlen==1)
	{
		IIC_set_ACKBT();
		
		*dat=RIIC0.ICDRR;
		while(!RIIC0.ICSR2.BIT.RDRF);
		
		RIIC0.ICSR2.BIT.STOP=0;
		RIIC0.ICCR2.BIT.SP = 1;
		*dat=RIIC0.ICDRR;
		RIIC0.ICMR3.BIT.WAIT=0;
		while(!RIIC0.ICSR2.BIT.STOP);
		RIIC0.ICSR2.BIT.NACKF=0;
		RIIC0.ICSR2.BIT.STOP=0;
		return ERROR;
	}
	if(datlen==2)
	{
		RIIC0.ICMR3.BIT.WAIT=1;
	}
	*dat=RIIC0.ICDRR;
	while(!RIIC0.ICSR2.BIT.RDRF);
	datlen--;
	while(datlen!=0)
	{
		if(datlen==2)
		{
			RIIC0.ICMR3.BIT.WAIT=1;
		}
		if(datlen==1)
		{
			IIC_set_ACKBT();
		}
		*(dat++)=RIIC0.ICDRR;
		while(!RIIC0.ICSR2.BIT.RDRF);
		datlen--;
	}

	RIIC0.ICSR2.BIT.STOP=0;
	RIIC0.ICCR2.BIT.SP = 1;
	*dat=RIIC0.ICDRR;
	RIIC0.ICMR3.BIT.WAIT=0;
	while(!RIIC0.ICSR2.BIT.STOP);
	RIIC0.ICSR2.BIT.NACKF=0;
	RIIC0.ICSR2.BIT.STOP=0;
	return OK;
}
